﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Beetle;

namespace BugVisual.Controllers
{
    public class DimaBugController : Controller
    {
        static Target[] targets = new Target[]{ new Target(new Position(100, 100), 1)};
        //static Bettle bug = new Bettle("foxiz");
        static Bettle bug = new Bettle("foxiz", "SuperNeuro.xml");

        public JsonResult NewTarget(int X, int Y, bool IsFood)
        {
            int i;
            if (IsFood) i = 0;
            else i = 1;
            targets[i].coord.x = X;
            targets[i].coord.y = Y;
            return Json(new { }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult NextIteration()
        {
            int counter = 50;

            while (counter > 0)
            {
                bug.MakeStepWithLearning(targets);
                //bug.CaclcError();
                counter--;
            }
           
            List<object> legs = new List<object>();
            for(int i=0;i<bug.Legs.Length;i++)
            {
                int angle=0;
                if (bug.Legs[i].State == 0) angle = -15;
                if (bug.Legs[i].State == 1) angle = 0;
                if (bug.Legs[i].State == 2) angle = 15;
                legs.Add(new {
                    IsUp=(bug.Legs[i].IsUp==1)?true:false,
                    angle=angle,
                    legId=i+1
                });
            }
            return Json(new
            {
                x = bug.position.x,
                y = bug.position.y,
                angle = bug.position.ViewAngle,
                legs = legs
            }, JsonRequestBehavior.AllowGet);       
        }

        #region Errors

        public class pmErrors
        {
            public string[] labels { get; set; }
            public double[] data { get; set; }
        }

        public JsonResult GetErrors()
        {
            bug.CaclcError();

            var result = bug.ErrorList().ToArray();

            var pm = new pmErrors();


            pm.data = result;

            if (result.Count() > 500)
            {
                List<double> tempData = new List<double>();
                int koef = (result.Count() / 500) +1;


                int counter = 0;
                while (counter < result.Count()) 
                {
                    tempData.Add(result[counter]);
                    counter += koef;
                }

                pm.data = tempData.ToArray();
            }



            string[] temp = new string[pm.data.Count()];

            for (int i = 0; i < temp.Count(); i++) temp[i] = "";

            pm.labels = temp;

            return Json(pm, JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region Index

        public class Model
        {
            public Position food {get;set;}
            public Position bug { get; set; }
        }

        public ActionResult Index()
        {       
            var pm = new Model();
            pm.food = targets[0].coord;
            pm.bug =bug.position;
            return View(pm);
        }

        #endregion

        public JsonResult ResetPosition()
        {
            bug.position = new Position(300,300){ViewAngle = 15};

            return Json(1,JsonRequestBehavior.AllowGet);
        }

    }
}
